package com.iotproject.studyservice.controller;


import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyGrade;
import com.iotproject.studyservice.entity.StudyWord;
import com.iotproject.studyservice.entity.vo.UnrememberWord;
import com.iotproject.studyservice.service.StudyGradeService;
import com.iotproject.studyservice.service.StudyWordService;
import com.iotproject.studyservice.service.UserUnrememberWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户生词本 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-04-07
 */
@RestController
@RequestMapping("/studyservice/user-unremember-word")
public class UserUnrememberWordController {

    @Autowired
    StudyWordService studyWordService;

    @Autowired
    StudyGradeService studyGradeService;

    @Autowired
    UserUnrememberWordService userUnrememberWordService;

    //点击不认识单词
    @GetMapping("unrememberWord/{wordId}")
    public R unrememberWord(@PathVariable("wordId") String wordId, HttpServletRequest httpServletRequest) {
        //展示单词意思
        StudyWord studyWord = studyWordService.getById(wordId);
        //查询等级名称
        String grade = studyWord.getGrade();
        StudyGrade studyGrade = studyGradeService.getById(grade);
        String gradeName = studyGrade.getName();
        //展示进度条信息
        Map<String, Double> progress = studyWordService.progress(grade, httpServletRequest);
        Double rememberPercent = progress.get("rememberPercent");
        Double forgetPercent = progress.get("forgetPercent");
        //加入生词本
        userUnrememberWordService.insertUserUnrememberWord(wordId, grade, httpServletRequest);
        return R.ok().data("gradeName", gradeName).data("word", studyWord).data("rememberPercent", rememberPercent).data("forgetPercent", forgetPercent);
    }

    //获取生词本单词列表
    @GetMapping("getUserUnrememberWordList/{grade}")
    public R getUserUnrememberWordList(@PathVariable("grade") String grade, HttpServletRequest httpServletRequest) {
        String memberUserId = studyWordService.getMemberUserId(httpServletRequest);
        List<UnrememberWord> userUnrememberWordList = userUnrememberWordService.getUserUnrememberWordList(memberUserId, grade);
        return R.ok().data("list", userUnrememberWordList);
    }

    //删除用户生词本单词
    @DeleteMapping("delectUserUnrememberWord/{wordId}")
    public R delectUserUnrememberWord(@PathVariable("wordId") String wordId) {
        boolean delectUserUnrememberWord = userUnrememberWordService.removeById(wordId);
        if (delectUserUnrememberWord) {
            return R.ok();
        }
        return R.error();
    }


}

